<?php

namespace App\Services;


use App\Models\ElectricCollector;
use App\Models\ElectricElectricity;
use Illuminate\Support\Facades\Artisan;
use Psy\Command\Command;

class ElectricService extends BaseService
{
    /**
     * Notes: 保电与拉合闸
     * User: lstink
     * Date: 2021/3/19
     * Time: 10:11 上午
     * @param $param
     * @return array
     */
    public function changeControl($param)
    {
        $id = $param['id'];
        switch ($param['type']) {
            case 1:
                $str_val = $param['is_protect'];
                $str = 'is_protect';
                if ($str_val == '1') {
                    $me = "启动保电成功";
                } else {
                    $me = "取消保电成功";
                }
                try {
                    ElectricElectricity::query()->where(['id' => $id])->update([$str => $str_val, 'update_time' => date('Y-m-d H:i:s', time())]);
                } catch (\Exception $e) {
                    return $this->format_error($e->getMessage());
                }
                return $this->format([], $me);
                break;
            case 2:
                $str_val = $param['is_close'];
                $code = $param['code'];
                $this_electric = ElectricElectricity::query()->selectRaw('id,is_protect,is_close,collect_id')->where(['tableAddress' => $code])->first();
                $count = ElectricElectricity::query()->where(['collect_id' => $this_electric['collect_id'], 'is_report' => 1])->count();
                if ($count > 0) {
                    return $this->format_error('尚有设备未添加入网关，请到采集器处上报');
                }
                if (empty($this_electric)) {
                    return $this->format_error('电表设备不存在');
                }
                $collect_code = ElectricCollector::query()->where(['id' => $this_electric['collect_id']])->value('code');
                if ($this_electric['is_protect'] == 1 && $str_val == 2) {
                    return $this->format_error('保电状态下不可以进行断闸操作');
                }
                if ($str_val == 1) {
                    $pa = 'on';
                } else {
                    $pa = 'off';
                }
                $result = Artisan::call('operating_equipment', ['electric_code' => $code, 'handle' => $pa, 'device_type' => "1", 'collect_id' => $collect_code]);
                sleep(4);//等待命令执行时间
                $after_operate = ElectricElectricity::query()->selectRaw('is_close,id')->where('id', $this_electric['id'])->first();
                if (($after_operate['is_close'] == 1 && $pa == "off") || ($after_operate['is_close'] == 2 && $pa == "on")) {//如果当前表状态为合闸状态但操作指令为拉闸，或者当前表状态为合闸单指令为拉闸
//                    $this->recordValveControl($code);
                    return $this->format_error('拉合闸失败');
                }
                if ($result == 0) {
                    return $this->format([] ,'发送操作指令成功');
                } else {
                    return $this->format_error('拉合闸失败');
                }
        }
    }

    /**
     * Notes: 控制信息
     * User: lstink
     * Date: 2021/3/19
     * Time: 10:59 上午
     * @param $id
     * @return array
     */
    public function controlInfo($id): array
    {
        $info = ElectricElectricity::query()->where('id', $id)->select(['is_protect', 'is_close'])->first();
        return $info ? $this->format($info) : $this->format_error('数据不存在');
    }


}
